package com.zeei.ems.base.controller;

import com.zeei.ems.base.constant.GlobalConstant;
import com.zeei.ems.base.result.ResultBasicVO;
import com.zeei.ems.base.result.ResultDataVO;
import com.zeei.ems.base.result.ResultRowsVO;
import com.zeei.ems.base.service.UserService;
import com.zeei.ems.base.util.SessionHelper;
import com.zeei.ems.base.util.StringUtil;
import com.zeei.ems.base.vo.UserParam;
import com.zeei.ems.base.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@Api(tags = "用户Controller")
@RequestMapping("/user")
public class UserController extends BasicController {

    @Autowired
    UserService userService;

    @ApiOperation(value = "获取用户列表")
    @PostMapping(value = {"/list/{pageSize}/{pageNum}", "/list"})
    public ResultRowsVO<UserVO> userList(@PathVariable(value = "pageSize", required = false) Integer pageSize,
                                         @PathVariable(value = "pageNum", required = false) Integer pageNum,
                                         UserParam userParam) {
        List<UserVO> userList = userService.listUser(pageSize, pageNum, userParam);
        return this.createResultRowsVO(userList, GlobalConstant.SUCCEED, userList.size());

    }


/*    @GetMapping(value = {"/exist/loginname/{loginname}", "/exist/loginname/{loginname}/{userId}"})
    public ResultDataVO<Boolean> loginNameExsit(@PathVariable String loginname, @PathVariable(required = false) Long userId) {
        return userService.loginNameExsit(loginname, userId);
    }*/

    @ApiOperation(value = "获取当前用户信息")
    @GetMapping("/info/session")
    public ResultDataVO<UserVO> getUserBySession(){
        return this.createResultDataVO(SessionHelper.getUser(), Boolean.TRUE);
    }

    @ApiOperation(value = "新增用户")
    @PostMapping("/add")
    public ResultBasicVO addUser(@RequestBody UserVO user) {
        Boolean result = userService.addUser(user);
        return this.createResultBasicVO(result, StringUtil.empty());
    }

    @ApiOperation(value = "更新用户")
    @PutMapping("/update")
    public ResultBasicVO updateUser(@RequestBody UserVO user) {
        Boolean result = userService.updateUser(user);
        return this.createResultBasicVO(result, StringUtil.empty());
    }

    @ApiOperation(value = "删除用户")
    @DeleteMapping(value = {"/delete/{userId}", "/delete"})
    public ResultBasicVO deleteUser(@PathVariable(value = "userId",required = false) Long userId,
                                   @RequestBody List<Long> userIds) {
        Boolean result = userService.deleteUser(userId, userIds);
        return this.createResultBasicVO(result, StringUtil.empty());
    }
}
